package com.lancoo.ccas53.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.TaskProcess;
import com.lancoo.ccas53.mapper.TaskProcessMapper;
import com.lancoo.ccas53.service.TaskProcessService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 各节点设置情况 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2024-05-21
 */
@Service
public class TaskProcessServiceImpl extends ServiceImpl<TaskProcessMapper, TaskProcess> implements TaskProcessService {
    @Resource
    private TaskProcessMapper taskProcessMapper;

    public static Map<String, Integer> processMap = new LinkedHashMap<>();

    static {

        processMap.put("基础信息", 1);
        processMap.put("基础信息-教学时间安排", 1);
        processMap.put("基础信息-教学时间安排-课时方案设置", 1);
        processMap.put("基础信息-教学时间安排-教学活动安排", 1);
        processMap.put("基础信息-教学时间安排-节假日安排", 1);
        processMap.put("基础信息-校区信息设置", 1);
        processMap.put("基础信息-校区信息设置-场地信息设置", 1);
        processMap.put("基础信息-校区信息设置-场地分配", 1);
        processMap.put("基础信息-校区信息设置-学生信息设置", 1);
        processMap.put("基础信息-校区信息设置-教师信息设置", 1);

        processMap.put("开课计划", 1);
        processMap.put("开课计划-确定教学计划", 1);
        processMap.put("开课计划-确定教学计划-主修", 1);
        processMap.put("开课计划-确定教学计划-辅修/第二专业", 1);
        processMap.put("开课计划-申请开课变更", 1);
        processMap.put("开课计划-审核开课变更", 1);
        processMap.put("开课计划-确认开课变更", 1);

        processMap.put("预选课程", 1);
        processMap.put("预选课程-设置预选课课程", 1);
        processMap.put("预选课程-学期预选课要求", 1);
        processMap.put("预选课程-设置预选课轮次", 1);
        processMap.put("预选课程-设置预选课条件", 1);
        processMap.put("预选课程-设置预选课条件-禁选课学生", 1);
        processMap.put("预选课程-设置预选课条件-选课上限", 1);
        processMap.put("预选课程-设置预选课条件-课程限制", 1);
        processMap.put("预选课程-设置预选课条件-跨专业选课学生", 1);
        processMap.put("预选课程-学生网上选课", 1);
        processMap.put("预选课程-查看预选课日志", 1);
        processMap.put("预选课程-确定预选课结果", 1);

        processMap.put("教学任务", 1);
        processMap.put("教学任务-设置选课课程", 1);
        processMap.put("教学任务-设置课程环节参数", 1);
        processMap.put("教学任务-生成教学班", 1);
        processMap.put("教学任务-分组上课", 1);
        processMap.put("教学任务-确定教学任务", 1);

        processMap.put("课表编排", 1);
        processMap.put("课表编排-排课规则设置", 1);
        processMap.put("课表编排-排课规则设置-基础规则", 1);
        processMap.put("课表编排-排课规则设置-场地禁排", 1);
        processMap.put("课表编排-排课规则设置-课程禁排优先排", 1);
        processMap.put("课表编排-排课规则设置-教师禁排优先排", 1);
        processMap.put("课表编排-编排课表", 1);
        processMap.put("课表编排-查看学期课表", 1);

        processMap.put("在线选课", 1);
        processMap.put("在线选课-学期选课要求", 1);
        processMap.put("在线选课-设置选课轮次", 1);
        processMap.put("在线选课-设置选课条件", 1);
        processMap.put("在线选课-设置预选课条件-禁选课学生", 1);
        processMap.put("在线选课-设置预选课条件-选课上限", 1);
        processMap.put("在线选课-设置预选课条件-课程限制", 1);
        processMap.put("在线选课-设置预选课条件-跨专业选课学生", 1);
        processMap.put("在线选课-学生网上选课", 1);
        processMap.put("在线选课-代学生选课", 1);
        processMap.put("在线选课-查看选课日志", 1);
        processMap.put("在线选课-确认选课结果", 1);
        processMap.put("教学任务-拆班上课", 1);
    }


    /**
     * @param taskId 任务id
     */
    @Override
    @Async
    @Transactional
    public void initProcess(int taskId) {
        List<TaskProcess> list = new ArrayList<>();
        //开课计划
        processMap.forEach((key, value) -> {
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        Integer count = 1;
        for (TaskProcess taskProcess : list) {
            taskProcess.setRecord(count);
            count++;
        }
        this.saveBatch(list);
    }

    @Override
    public List<TaskProcess> getProcessList(int taskId) {
        List<TaskProcess> processList = this.list(new LambdaQueryWrapper<TaskProcess>().eq(TaskProcess::getTaskId, taskId));
        return processList;

    }

    @Override
    public Boolean updateProcess(int record, int taskId, int finish) {
        LambdaUpdateWrapper<TaskProcess> updateWrapper = new LambdaUpdateWrapper<TaskProcess>()
                .eq(TaskProcess::getTaskId, taskId)
                .eq(TaskProcess::getRecord, record)
                .set(TaskProcess::getIsFinish, finish);
        boolean update = this.update(updateWrapper);
        return update;
    }

    @Override
    public TaskProcess getProcess(int record, int taskId) {
        TaskProcess result = this.lambdaQuery()
                .eq(TaskProcess::getTaskId, taskId)
                .eq(TaskProcess::getRecord, record)
                .one();
        return result;
    }


}
